package ormsamples;

import org.orm.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class SampleServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		try {
			if (action.equals("create")) {
				ORMDatabaseInitiator.createSchema(com.estate.datalogic.EstatePersistentManager.instance());
			}
			else if (action.equals("drop")) {
				ORMDatabaseInitiator.dropSchema(com.estate.datalogic.EstatePersistentManager.instance());
			}
			else {
				if (action.equals("insert")) {
					CreateEstateData createEstateData = new CreateEstateData();
					createEstateData.createTestData();
				}
				else if (action.equals("update")) {
					RetrieveAndUpdateEstateData retrieveAndUpdateEstateData = new RetrieveAndUpdateEstateData();
					retrieveAndUpdateEstateData.retrieveAndUpdateTestData();
				}
				else if (action.equals("delete")) {
					DeleteEstateData deleteEstateData = new DeleteEstateData();
					deleteEstateData.deleteTestData();
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		//***** To ensure persistent session is closed after every request, either add the following lines to "Web.xml" (recommended):
		//
		//<filter>
		//  <filter-name>ORMFilter</filter-name>
		//	<filter-class>ormsamples.EstateFilter</filter-class>
		//</filter>
		//
		//<filter-mapping>
		//	<filter-name>ORMFilter</filter-name>
		//	<url-pattern>/*</url-pattern>
		//</filter-mapping>
		//
		//***** or add the following statement at the end of each servlet that used ORM:
		//
		//com.estate.datalogic.EstatePersistentManager.instance().getSession().close();", true);
		
		PrintWriter lWriter = response.getWriter();
		lWriter.println("Sample Servlet. Action = " + action);
		lWriter.close();
	}
}
